import datetime
from flask import Blueprint,request,jsonify
import requests

from views.db import Db

import json

from utils.jwtc import myjwt

from utils.Make_password import make_password

from utils.myfile import MyFile

from utils.redis import rs

from utils.acl import ACL

import redis

import re

from views.db import db

from utils.acl import  acl

from utils.myfile import get_data

import time

bp_user = Blueprint("user",__name__)



@bp_user.route('/loginM',methods = ['POST','GET'])
def loginM():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        print(data)
        sql = """select * from user where username = '%s'"""%(data['username'])
        res = db.get_one(sql)
        print(sql)
        print(res)
        if res:
            if res['password'] == data['password']:
                uid = res['id']
                token = myjwt.encode_jwt({'data':{'uid':res['id']}})
                ress = acl.get_vue(uid)
                # 把url存入redis,
                rs.insert_set('users'+str(uid),ress)
                aa = rs.find_set('users'+str(uid))
                print(aa)
                return jsonify({'code':200,"mes":"登陆成功","token":token,'uid':uid,"url":aa})
            else:
                return jsonify({'code': 20021, "mes": "密码错误"})
        else:
            return jsonify({'code': 20021,"mes":"账户不存在"})

#构建2层分类
def getresdata(data):
    # 把顶级拿出来放到列表中
    list = []
    idlist=[]
    for i in data:
        id = i['pid']
        if id not in idlist:
            list.append({'id':id,'name':i['pname'],'son':[]})
            idlist.append(id)
    # 变理顶级列表，在遍历数据，把子类加到顶级列表
    for j,value in enumerate(list):

        for i in data:
        
            if value['id'] == i['pid']:
                
                list[j]['son'].append({'id':i['id'],'name':i['name'],'url':i['url']})
    return list



@bp_user.route('/getResource')

def getResource():

    uid = 1

    res = acl.get_resourcelist(uid)

    mes = getresdata(res)

    return jsonify({'list':mes})





  

        
                   
                   
                    
            
